package net.lesscoding.consumer;

import net.lesscoding.common.Const;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;

/**
 * @author eleven
 * @date 2024/6/1 14:19
 * @apiNote
 */
public class BatchConsumer {
    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("cg");
        consumer.setNamesrvAddr(Const.TS_NAME_SRV_ADDR_OS1);
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        consumer.subscribe("BatchTopic", "*");
        // 指定每次可以消费10条消息，默认为1
        consumer.setConsumeMessageBatchMaxSize(10);
        // 指定每次拉取40条消息, 默认是32
        consumer.setPullBatchSize(40);
        consumer.registerMessageListener(
                (MessageListenerConcurrently) (msgs, context) -> {
                    for (MessageExt msg : msgs) {
                        System.out.println(msg);
                    }
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                });
        consumer.start();
        System.out.println("Consumer Started!");
    }
}
